﻿using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.PowerPlanModels;
using Quartz;
using System;

namespace DevelopTool.Bll.PowerPlanBlls
{
    /// <summary>
    /// 电源计划job
    /// </summary>
    public class PowerPlanJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                double nowCPUUsage = WindowsHelper.GetCPUTotalUsage();//当前cpu使用率

                if (string.IsNullOrEmpty(Setting.PowerPlan.DefaultModelPlan) || Setting.PowerPlan.DefaultModelPlan == "0") return;
                if (string.IsNullOrEmpty(Setting.PowerPlan.LeaveModePlan) && string.IsNullOrEmpty(Setting.PowerPlan.SpeedupModePlan)) return;
                if (Setting.PowerPlan.LeaveModePlan == "0" && Setting.PowerPlan.SpeedupModePlan == "0") return;

                string defaultModelPlan = Setting.PowerPlan.DefaultModelPlan;//默认电源计划
                string leaveModePlan = Setting.PowerPlan.LeaveModePlan;//离开模式电源计划
                string speedupModePlan = Setting.PowerPlan.SpeedupModePlan;//加速模式电源计划
                int idleTimeout = Setting.PowerPlan.IdleTimeout;//键鼠空闲时间
                int cpuUsage = Setting.PowerPlan.CPUUsage;//CPU使用率阈值

                Powercfg nowPowercfg = PowerPlanBll.GetCurrentPowercfg();//当前电源计划
                
                long nowIdleTimeSeconds = WindowsHelper.GetIdleTimeSeconds();//当前键鼠空闲时间

                if (leaveModePlan != "0")
                {
                    if (nowIdleTimeSeconds >= idleTimeout)//鼠标、键盘空闲时间超过设定时间
                    {
                        //是否已开启加速模式
                        if (speedupModePlan != "0" && nowCPUUsage >= cpuUsage)
                        {
                            if (nowPowercfg.UUID != speedupModePlan)
                            {
                                Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(speedupModePlan);
                                PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"CPU使用率达到:{nowCPUUsage}，电源计划已从【{nowPowercfg.Name}】切换为：[加速模式]【{newPowercfg?.Name}】"));
                                PowerPlanBll.TempTimeSeconds = 0;
                            }
                            else PowerPlanBll.TempTimeSeconds++;
                        }
                        else if (nowPowercfg.UUID != leaveModePlan)
                        {
                            Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(leaveModePlan);
                            PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"鼠标键盘已空闲:{nowIdleTimeSeconds}秒，电源计划已从【{nowPowercfg.Name}】切换为：[离开模式]【{newPowercfg?.Name}】"));
                            PowerPlanBll.TempTimeSeconds = 0;
                        }
                        else PowerPlanBll.TempTimeSeconds++;
                    }
                    else
                    {
                        if (nowPowercfg.UUID != defaultModelPlan)
                        {
                            Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(defaultModelPlan);
                            PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"保持【{nowPowercfg.Name}】电源计划{PowerPlanBll.TempTimeSeconds * 2}秒，已还原为[默认]电源计划【{newPowercfg.Name}】"));
                            PowerPlanBll.TempTimeSeconds = 0;
                        }
                    }
                }


                if (speedupModePlan != "0")
                {
                    if (nowCPUUsage >= cpuUsage)//cpu使用率已达到阈值
                    {
                        if (nowPowercfg.UUID != speedupModePlan)
                        {
                            Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(speedupModePlan);
                            PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"CPU使用率达到:{nowCPUUsage}，电源计划已从【{nowPowercfg.Name}】切换为：[加速模式]【{newPowercfg?.Name}】"));
                            PowerPlanBll.TempTimeSeconds = 0;
                        }
                        else PowerPlanBll.TempTimeSeconds++;
                    }
                    else if (leaveModePlan != "0" && nowIdleTimeSeconds >= idleTimeout)
                    {
                        if (nowPowercfg.UUID != leaveModePlan)
                        {
                            Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(leaveModePlan);
                            PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"鼠标键盘已空闲:{nowIdleTimeSeconds}秒，电源计划已从【{nowPowercfg.Name}】切换为：[离开模式]【{newPowercfg?.Name}】"));
                            PowerPlanBll.TempTimeSeconds = 0;
                        }
                        else PowerPlanBll.TempTimeSeconds++;
                    }
                    else
                    {
                        if (nowPowercfg.UUID != defaultModelPlan)
                        {
                            Powercfg newPowercfg = PowerPlanBll.ChangePowerPlan(defaultModelPlan);
                            PowerPlanLogBll.AddLog(new LogModel(LogType.Info, $"保持【{nowPowercfg.Name}】电源计划{PowerPlanBll.TempTimeSeconds * 2}秒，已还原为[默认]电源计划【{newPowercfg.Name}】"));
                            PowerPlanBll.TempTimeSeconds = 0;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                PowerPlanLogBll.AddLog(new LogModel(LogType.Error, $"任务内部异常：{ex.Message}"));
            }
        }


    }
}
